﻿using AutoMapper;
using HealthcareSystem.API.Write.Application.Command.ChronicDisease;
using HealthcareSystem.Domain.ChronicDiseaseManagement;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.ChronicDisease
{
    public class HealthEducationDeleteCommandHandler : IRequestHandler<HealthEducationDeleteCommand, ApiResult<int>>
    {

        private readonly IBaseRepository<HealthEducation> heRepository;

        private readonly IMapper mapper;

        public HealthEducationDeleteCommandHandler(IBaseRepository<HealthEducation> heRepository, IMapper mapper)
        {
            this.heRepository = heRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(HealthEducationDeleteCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ResultCode.删除成功;
            result.Message = ResultCode.删除成功.ToString();
            var list = heRepository.GetAll().Where(x => x.EducationId == request.EducationId).FirstOrDefault();
            if (list!=null)
            {
                list.IsDelete = true;
                await heRepository.UpdAsync(list);
            }
            result.Data = list.EducationId;
            return await Task.FromResult(result);
        }
    }
}
